﻿using System;
using System.Collections;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO.Ports;
using SMS.DAO;
namespace eTMS.Services
{
    public class SMSService : ISMSService
    {
        public bool SendSMSMessage(string PhoneNumber, string SMSContent)
        {
            bool result = true;
            SMSDAO mySMS = new SMSDAO();
            SerialPort port = new SerialPort();
            port = mySMS.OpenPort(System.Configuration.ConfigurationSettings.AppSettings["smsPortName"], Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["smsBaudRate"]), Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["smsDataBits"]), Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["smsReadTimeOut"]), Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["smsWriteTimeOut"]));
            try
            {
                if (port != null)
                {
                    if (SMSContent.Length > 160)
                    {
                        try
                        {
                            result = mySMS.sendMsg(port, PhoneNumber, SMSContent.Substring(0, 150) + "...");
                        }
                        catch
                        { result = false; }
                    }
                    else
                    {
                        try
                        {
                            result = mySMS.sendMsg(port, PhoneNumber, SMSContent);
                        }
                        catch
                        { result = false; }
                    }


                    mySMS.ClosePort(port);
                }
            }
            catch
            {
                result = false;
            }
            return result;
        }
        public bool SendListSMSMessage(List<SenderContext> list)
        {
            bool bresult = true;

            try
            {
                SMSDAO mySMS = new SMSDAO();
                SerialPort port = new SerialPort();
                port = mySMS.OpenPort(System.Configuration.ConfigurationSettings.AppSettings["smsPortName"], Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["smsBaudRate"]), Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["smsDataBits"]), Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["smsReadTimeOut"]), Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["smsWriteTimeOut"]));
                System.Threading.Thread.Sleep(1000);
                if (port != null)
                {
                    foreach (SenderContext sc in list)
                    {
                        try
                        {
                            if (sc.Context.Length > 160)
                            {
                                bresult &= mySMS.sendMsg(port, sc.Number, sc.Context.Substring(0, 150) + "...");
                            }
                            else
                            {
                                bresult &= mySMS.sendMsg(port, sc.Number, sc.Context);
                            }

                        }
                        catch { bresult = false; }
                        System.Threading.Thread.Sleep(1000);
                    }
                    mySMS.ClosePort(port);
                }
            }
            catch
            {
                bresult = false;
            }
            return bresult;
        }

    }
}